<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/26
 * Time: 21:49
 */

namespace app\models;

use think\Model;

class MerchantModel extends Model
{
	/**
	 * 商户信息表
	 * @var string
	 */
	protected $table = 'edu_merchant';

	/**
	 * 商户信息列表
	 *
	 * @param $param
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function getMerchantLists($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		if (isset($param['mch_name']) && '' !== $param['mch_name']) {
			$where['mch_name'] = ['like', $param['mch_name'].'%'];
		}
		if (isset($param['status']) && '' !== $param['status']) {
			$where['mch_status'] = $param['status'];
		}
		if (isset($param['company_id']) && '' !== $param['company_id']) {
			$where['company_id'] = $param['company_id'];
		}
//		if(get_current_type() == 2){
//			$where['company_id'] = get_current_company_id();
//		}
		$listData = self::where($where)->order('id desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$item['status_text'] = (1 === $item['mch_status']) ? '<span style="color: #00B83F">已启用</span>' : '<span style="color: #cc0000">已禁用</span>';
				$item['company_name'] = (new CompanyModel())->get($item['company_id'])['company_name'];
				$item['campus_name'] = (new CampusModel())->get($item['campus_id'])['campus_name'];
				$item['mp_name'] = (new MpModel())->get($item['mp_id'])['mp_name'];
			}
		}

		return $listData;
	}
}